package manager
{
	import event.EventManager;
	import event.GameEvent;
	
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	import flash.utils.setTimeout;
	
	import util.ScreenUtil;
	
	import valueObject.MonsterVO;
	import valueObject.TowerVO;

	/**
	 *炮塔管理 
	 * @author SXT
	 * 
	 */
	public class TowerManager extends BaseManager implements IFram
	{
		/**
		 *怪物对象池 
		 */
		private var pool:Vector.<TowerVO>;
		/**
		 *当前关卡配置 
		 */
		private var levelVO:LevelVO;
		/**
		 *与本类有关的配置文件 
		 */		
		private var config:Dictionary;
		
		private var index:int;
		/**
		 *建造时的矩形框 
		 */		
		private var buildRectangle:Shape;
		/**
		 *是否是建造状态 
		 */		
		private var buildingVO:TowerVO;
		
		public function TowerManager()
		{
			super();
			MAX = 30;
			pool = new Vector.<TowerVO>;
			levelVO = Game.instance.main.levelManager.levelvo;
			EventManager.instance.addEventListener(GameEvent.GAME_BUILD_TOWER,buildTower);
			
			buildRectangle = new Shape();
			buildRectangle.graphics.beginFill(0x00ff00);//sh.图形属性，填充颜色
			buildRectangle.graphics.drawRect(0,0,30,30);//sh.图形属性，画矩形
			buildRectangle.graphics.endFill();
			buildRectangle.alpha = 0.5;
		}
		
		public function updataConfig(level:int):void{
			config =  Game.instance.main.conl["configDic"]["tower"];
		}
		
		protected function buildTower(e:GameEvent):void
		{
			buildingVO = addTower(e.data.id);
			setBuildingState(true);
			setTimeout(function():void{Game.instance.main.addEventListener(MouseEvent.CLICK,cancleBuild)},300);
		}
		
		protected function cancleBuild(e:Event):void
		{
			setBuildingState(false);
			Game.instance.main.removeEventListener(MouseEvent.CLICK,cancleBuild);
			buildingVO.building = false;
			buildingVO.x = Game.instance.main.mouseX;
			buildingVO.y = Game.instance.main.mouseY;
			Game.instance.mtLayer.addChild(buildingVO);
		}
		
		/**
		 * 设置建造模式 
		 * @param value
		 */
		private function setBuildingState(value:Boolean=true):void{
			if(value){
				Game.instance.mtLayer.addChild(buildRectangle);
			}else{
				if(Game.instance.mtLayer.contains(buildRectangle)){
					Game.instance.mtLayer.removeChild(buildRectangle);
				}
			}
		}
		
		public function framUpdata(index:int):void
		{
			for each(var tmp:TowerVO in pool){
				if((tmp == null) || tmp.building) continue;
				for each(var tmp1:MonsterVO in Game.instance.monsterMag.pool){
					if(tmp1.isActivity){
//						trace(ScreenUtil.getDisByDis(tmp,tmp1));//测距
						if(ScreenUtil.getDisByDis(tmp,tmp1) <= tmp.range){
							if(tmp.target == null){													//如果攻击目标是空，则寻找新目标
								tmp.target = tmp1;														// 设置目标
							}
							tmp.fire();																		//距离内就开火
							tmp.setDirection(tmp1.x - tmp.x, tmp1.y - tmp.y);
						}else{
							tmp.target = null;															//超出范围则把目标制空
						}
					}else{
						if(tmp.target){
							if(tmp.target == tmp1){
								tmp.target = null;
							}
						}
					}
				}
			}
			if(buildingVO){
				buildRectangle.x = Game.instance.main.mouseX;
				buildRectangle.y = Game.instance.main.mouseY;
			}
		}
		
		public function addTower(id:int):TowerVO{
			var tower:TowerVO;
			if(pool.length > MAX){
				tower = pool[index];
				if(index>19){
					index = 0;
				}else{
					index++;
				}
			}else{
				tower = new TowerVO();
				pool.push(tower);
			}
			tower = getConfig(String(id),tower);
			tower.id = id;
			tower.init();
			
			return tower;
		}
		
		/**
		 *读取配置文件中对应的属性 
		 * @param id	怪物的id	
		 * @param monster
		 * @return 
		 */
		private function getConfig(id:String,tower:TowerVO):TowerVO{
			if(levelVO.towers.indexOf(id) != -1){									//检查id是否在当前的关卡配置中
				for (var tmp:String in  config[id]){
					try{
						tower[tmp] =config[id][tmp];
					}catch(e:Error){
						try{
							if(String(config[id][tmp]).indexOf("#") != -1){
								tower[tmp] = String(config[id][tmp]).split("#");
							}else{
								tower[tmp].push(config[id][tmp]);
							}
						}catch(e:Error){
						}
//						trace(e.message);
//						trace("monster"+"的"+tmp+"属性不存在，请核对配置文件或变量命名" );
					}
				}
				tower.x = levelVO.startPoint[0];
				tower.y = levelVO.startPoint[1];
				tower.mc = new tower10001();
				tower.addChild(tower.mc);
			}
			return tower;
		}
	}
}